package com.cray.intelligenthydrologyback.web;

import com.cray.intelligenthydrologyback.client.domain.entity.SysConfig;
import com.cray.intelligenthydrologyback.client.domain.entity.SysDictData;
import com.cray.intelligenthydrologyback.client.model.AbstractResponseResult;
import com.cray.intelligenthydrologyback.client.model.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author Emil
 */
@Controller
@ResponseBody
@RequestMapping("/system")
@Api(value = "Sys Dict Data Controller", tags = "数据字典信息配置")
public class SystemDictDataController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @Autowired
    private ObjectMapper objectMapper;

    @PutMapping("/dict/data")
    @ApiOperation(httpMethod = "PUT", value = "修改保存字典类型")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult updateSystemDictData(
            @ApiParam(value = "device", required = true)
            @RequestBody SysDictData sysDictData) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().put()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/dict/data")
                        .build(sysDictData)) // 路径变量
                .bodyValue(sysDictData) // 请求体，可以是任何对象
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }

    @PostMapping("/dict/data")
    @ApiOperation(httpMethod = "POST", value = "增加")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Add system face info",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysDictData> addSystemDictData(
            @RequestBody SysDictData sysDictData) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()  // 使用 POST 请求
                .uri("/system/dict/data")  // 请求的 URL
                .bodyValue(sysDictData) // 请求体，可以是任何对象
                .retrieve() // 发送请求并获取响应
                .bodyToMono(String.class); // 响应内容转换为 String
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @GetMapping("/dict/data/{dictCode}")
    @ApiOperation(httpMethod = "GET", value = "根据参数编号获取详细信息")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysDictData> getSystemDictCode(
            @ApiParam(value = "id", required = true)
            @PathVariable(value = "dictCode") Long dictCode) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/dict/data/{dictCode}")
                        .build(dictCode)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @DeleteMapping("/dict/data/{dictCodes}")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public AbstractResponseResult deleteSystemDictCode(
            @ApiParam(value = "ids", required = true)
            @PathVariable(value = "dictCodes") Long dictCodes) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().delete()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/dict/data/{dictCodes}")
                        .build(dictCodes)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), AbstractResponseResult.class);
    }

    @PostMapping("/dict/data/export")
    @ApiOperation(httpMethod = "POST", value = "字典数据导出")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Add system face info",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysDictData> exportDictData(
            @RequestBody SysDictData sysDictData) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().post()  // 使用 POST 请求
                .uri("/system/dict/data/export")  // 请求的 URL
                .bodyValue(sysDictData) // 请求体，可以是任何对象
                .retrieve() // 发送请求并获取响应
                .bodyToMono(String.class); // 响应内容转换为 String
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @GetMapping("/dict/data/list")
    @ApiOperation(httpMethod = "GET", value = "字典数据分页查询列表")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> getSystemDictDataList() throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/dict/data/list")
                        .build()) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }

    @GetMapping("/dict/data/type/{dictType}")
    @ApiOperation(httpMethod = "GET", value = "根据字典类型查询字典数据信息")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<SysConfig> getSystemDictDataType(
            @ApiParam(value = "dictType", required = true)
            @PathVariable(value = "dictType") Long dictType) throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/system/dict/data/type/{dictType}")
                        .build(dictType)) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }
}
